//
//  PublicExtension.swift
//  OnemeetingDemo
//
//  Created by 张衡 on 2020/4/10.
//  Copyright © 2020 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation

func showNETFail(){
    MTToast.show(msg: "获取数据失败，检查您的网络，刷新请重试！".localized())
}

func showDataFail(){
    MTToast.show(msg: "网络请求不可用".localized())
}

func show_msg(_ msg:String){
    MTToast.show(msg: msg)
}

func showNoTitle_msg(_ msg:String){
    MTToast.show(msg: "立即更新")
}


extension String {
    
    func getIntFromString() -> String {
        let scanner = Scanner(string: self)
        scanner.scanUpToCharacters(from: CharacterSet.decimalDigits, into: nil)
        var number :Int = 0
        scanner.scanInt(&number)
        return String(number)
    }
    
    
    /// 整体处理成隔3加空格
    func stringToPhone() -> String {
        let text = self
        var newString = ""
        for i in 0 ..< text.count {
            if i%3==0 {
                newString.append(" ")
            }
            newString.append(text[i])
        }
        return newString
    }

    /// 实时输入单个字符时，自动添加空格
    func singleToPhone() -> String {
        var newString = self
        if (newString[newString.startIndex] == "0") {
            if  newString.count == 1||newString.count == 5||newString.count == 9 || newString.count == 13 {
                newString.append(" ")
            }
        }else {
            if newString.count == 3||newString.count == 7 || newString.count == 11 {
                newString.append(" ")
            }
        }
        return newString
    }
    
    /// 实时删除单个字符时，自动去掉空格
    @discardableResult
    func removeSingleToLast() -> String {
        var newString = self
        if (newString[newString.endIndex] == " ") {
            newString.remove(at: newString.endIndex)
            newString.removeSingleToLast()
        }
        return newString
    }
    
    func clearId() -> String {
        let text = self
        var newString = text.replacingOccurrences(of: "-", with: "")
        newString = newString.replacingOccurrences(of: " ", with: "")
        return newString
    }
    
    
}


extension NSNumber {
    func toString() -> String? {
        return NumberFormatter().string(from: self)
    }
}


extension String {
    func toNumber() -> NSNumber? {
        let num =  NumberFormatter()
        num.numberStyle = NumberFormatter.Style.decimal
        return num.number(from: self)
    }
    
}

class PE: NSObject {
    class func toString(obj:Any?) -> String? {
        if obj is String {
            return (obj as? String)
        }
        
        if obj is NSNumber {
            return (obj as? NSNumber)?.toString()
        }
        return nil
    }
}

protocol AnyObj: Any {}
extension AnyObj {
    func toString() -> String? {
        let obj = self
        if obj is String {
            return (obj as? String)
        }
        
        if obj is NSNumber {
            return (obj as? NSNumber)?.toString()
        }
        return nil
    }
}


struct timeInter {
    var timer:DispatchSourceTimer!
    var count:Int?
    init(timer:DispatchSourceTimer,count:Int?) {
        self.timer = timer
        self.count = count
    }
}


/// GCD定时器倒计时⏳
///   - timeInterval: 循环间隔时间
///   - repeatCount: 重复次数
///   - handler: 循环事件, 闭包参数： 1. timer， 2. 剩余执行次数

@discardableResult
public func dispatchTimer(timeInterval: Double, repeatCount:Int, handler:@escaping (DispatchSourceTimer?, Int)->()) -> DispatchSourceTimer?{
    
    if repeatCount <= 0 {
        return nil
    }
    let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main)
    
    var count = repeatCount
    timer.schedule(wallDeadline: .now(), repeating: timeInterval)
    timer.setEventHandler(handler: {
        count -= 1
        DispatchQueue.main.async {
            handler(timer, count)
        }
        if count == 0 {
            timer.cancel()
        }
        
    })
    timer.resume()
    return timer
}

/// GCD定时器循环操作
///   - timeInterval: 循环间隔时间
///   - handler: 循环事件
public func dispatchTimer(timeInterval: Double, handler:@escaping (DispatchSourceTimer?)->()) {
    let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main)
    timer.schedule(deadline: .now(), repeating: timeInterval)
    timer.setEventHandler {
        DispatchQueue.main.async {
            handler(timer)
        }
    }
    timer.resume()
}
